// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../BASE/ComponentBase";
import JJ from "./JJ";
const { ccclass, property } = cc._decorator;

@ccclass
export default class DoubleClickListener extends ComponentBase {
    @property
    private doubleClickInterval: number = 300; // 双击间隔时间，单位为毫秒
    @property
    private doubleClickThreshold: number = 10; // 双击位置的容忍度，单位为像素

    private lastClickTime: number = 0; // 上次点击时间
    private lastClickPosition: cc.Vec2 = cc.v2(0, 0); // 上次点击位置

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    onTouchStart(event: cc.Touch) {
        const now = Date.now();
        const touchPosition = this.node.convertToNodeSpaceAR(event.getLocation());

        // 检查是否满足双击条件：时间间隔和位置容忍度
        if (now - this.lastClickTime < this.doubleClickInterval &&
            (touchPosition.x - this.lastClickPosition.x) * (touchPosition.x - this.lastClickPosition.x) +
            (touchPosition.y - this.lastClickPosition.y) * (touchPosition.y - this.lastClickPosition.y) <
            this.doubleClickThreshold * this.doubleClickThreshold) {
            // 执行双击事件的回调
            this.onDoubleClick();
        }

        // 更新上次点击时间和位置
        this.lastClickTime = now;
        this.lastClickPosition = touchPosition;
    }

    onDoubleClick() {
   cc.find("Canvas/Main Camera").getComponent(JJ).HG(this.node)
    }}